package com.metaera.app.controller.view;

import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.metaera.app.R;
import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.data.model.SearchResponse;
import com.metaera.app.data.model.SearchResultResponse;
import com.metaera.app.ui.adapters.SearchResultActivityAdapter;
import com.metaera.app.ui.adapters.SearchResultChoiceTopicAdapter;
import com.metaera.app.ui.adapters.SearchResultColumnAdapter;
import com.metaera.app.ui.adapters.SearchResultFlashNewsAdapter;
import com.metaera.app.ui.adapters.SearchResultNewsAdapter;
import com.metaera.app.utils.event.OnSearchIndexEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class SynthesisSearchResultView implements INetworkRequestView<SearchResponse> {

    private ViewGroup layoutContainer, layoutEmpty;
    private String keyword;

    public SynthesisSearchResultView(ViewGroup layoutContainer, String keyword) {
        this.layoutContainer = layoutContainer.findViewById(R.id.layout_list);
        this.layoutEmpty = layoutContainer.findViewById(R.id.layout_state_container);
        this.keyword = keyword;
    }

    @Override
    public void successful(SearchResponse data) {
        Resources resources = layoutContainer.getContext().getResources();
        String[] strings = resources.getStringArray(R.array.search_types);

        layoutContainer.removeAllViews();

        if (isEmpty(data.getArticle_list()) && isEmpty(data.getFlash_list())
                && isEmpty(data.getSpecial_list()) && isEmpty(data.getColumn_list())
                && isEmpty(data.getActivity_list())) {

            layoutEmpty.setVisibility(View.VISIBLE);

        } else {

            layoutEmpty.setVisibility(View.GONE);
            init(data.getArticle_list(), new SearchResultNewsAdapter(keyword), strings[1], 1); // 文章
            init(data.getFlash_list(), new SearchResultFlashNewsAdapter(keyword), strings[2], 2); // 快讯
            init(data.getSpecial_list(), new SearchResultChoiceTopicAdapter(keyword), strings[3], 3); // 专题
            init(data.getColumn_list(), new SearchResultColumnAdapter(), strings[4], 4); // 专栏
            init(data.getActivity_list(), new SearchResultActivityAdapter(keyword), strings[5], 5); // 活动
        }
    }

    private <T> boolean isEmpty(List<T> list) {
        return list == null || list.isEmpty();
    }


    private void init(
            List<SearchResultResponse> list,
            BaseQuickAdapter<SearchResultResponse, BaseViewHolder> adapter,
            String title, int index) {
        if (list != null && !list.isEmpty()) {
            View inflate = LayoutInflater.from(layoutContainer.getContext())
                    .inflate(R.layout.include_synthesis_recycler_view, layoutContainer, false);
            TextView tvTitle = inflate.findViewById(R.id.tv_title);
            tvTitle.setText(title);
            RecyclerView recycler_view = inflate.findViewById(R.id.recycler_view);
            inflate.findViewById(R.id.tv_more).setOnClickListener(v -> EventBus.getDefault().post(new OnSearchIndexEvent(index)));
            recycler_view.setNestedScrollingEnabled(true);
            //SearchResultNewsAdapter adapter = new SearchResultNewsAdapter(keyword);
            adapter.addData(list);
            recycler_view.setAdapter(adapter);
            layoutContainer.addView(inflate);
        }
    }

}
